<?php

namespace brand\components;

use brand\logic\BrandHelper;
use brand\logic\Config;
use brand\tools\Functions;

class Date extends Component
{

    public function _init()
    {
        $this->component = 'date';
    }

    public function Form()
    {
        parent::Form();
        if ($this->is_search){
            //搜索 转时间区间
            $this->form_column->props(['type'=>'datetimerange']);
        }
        return $this->form_column;
    }


    public function buildProps()
    {
        parent::buildProps();
        if ($this->getFormParam('props.placeholder')){
            $this->config['form']['props']['placeholder'] = '请选择'.$this->label;
        }
    }

    public function checkFormValue()
    {
        if ($this->getFormParam('props.value-format') == 'timestamp'){
            $this->value = BrandHelper::value2time($this->value);
        }

        if ($this->getFormParam('props.type') == 'datetimerange' && $this->value) {
            //区间
            $this->value = BrandHelper::string2array($this->value,Config::ARRAY_DIVISION_TIME);

        }

        if ($this->value == 0) $this->value = "";
    }

    public function checkTableValue($key, $value)
    {
        $value = parent::checkTableValue($key, $value);
        $show_type = $this->getTableParam('show_type') ?: '';
        if($show_type == 'curd' || $show_type == ''){

            if ($this->getFormParam('props.value-format') == 'timestamp') {
                $value = BrandHelper::time2value($value);
            }

            if ($this->getFormParam('props.type') == 'datetimerange') {
                $value = BrandHelper::array2String($value,Config::ARRAY_DIVISION_TIME);
            }
        }
        return $value;
    }

    public function checkUpdateData($key, $value)
    {
        if ($this->getFormParam('props.value-format') == 'timestamp') {
            $value = BrandHelper::ms2time($value);
        }
        if ($this->getFormParam('props.type') == 'datetimerange') {
            //区间
            if (is_array($value)){
                foreach ($value as $key => $item) {
                    $value[$key] = BrandHelper::ms2time($item);
                }
                $value = BrandHelper::array2String($value,Config::ARRAY_DIVISION_TIME);
            }

        }
        return $value;
    }

    /**
     * 数据表转CURD
     * @param $field
     * @param $props
     */
    public function DbToCurd($field, $props=[])
    {
        $column = \brand\Brand::Builder()
            ->form_props($props)
            ->form_method('date')
            ->table_action_type('date')
            ->table($props)
            ->search_condition('between');
        //时间区间不支持搜索
        if ((isset($props['type']) && $props['type'] == 'datetimerange') || $field['name'] == Config::DELETE_TIME){
            $column->search(false);
        }

        if ($field['name'] == Config::CREATE_TIME){
            $column->form('create_time', ($props['value-format'] ?? 'timestamp') );
        }
        if ($field['name'] == Config::UPDATE_TIME){
            $column->form('update_time', ($props['value-format'] ?? 'timestamp') );
        }
        if ($field['name'] == Config::DELETE_TIME){
            $column->form('delete_time', ($props['value-format'] ?? 'timestamp') );
        }

        return $column;
    }

    /**
     * 构建搜索条件
     * @param $key
     * @param $param
     * @return array|false
     */
    public function buildCondition($key,$param)
    {
        [$field,$key] = $this->getSearchField($key);
        $condition = $this->config['search']['condition'] ?? '';


        if (isset($this->config['form']['props']['value-format']) && $this->config['form']['props']['value-format'] == 'timestamp') {
            if (!is_array($param)){
                $param = explode(',',$param);
            }
            //时间戳/1000
            if (is_array($param) && count($param) == 2) {
                foreach ($param as $index => $value) {
                    if($value) {
                        if (is_numeric($value)){
                            $param[$index] = (int)ceil($value/1000);
                        }else{
                            $param[$index] = strtotime($value);
                        }
                    }
                }
            }
        }

        return [$field . '.' .$key,$condition,$param];
    }

    public function buildConfigColumn($tableConfigValue, $curd_method = 'curd_method')
    {

        $options['type'] = \brand\Brand::Builder('日期类型','select')
            ->form_options(BrandHelper::formatOptions(['datetime'=>'日期时间','date'=>'日期','datetimerange'=>'日期区间']))->build();

        $options['value_format'] = \brand\Brand::Builder('日期格式','select')
            ->form_options(BrandHelper::formatOptions(['default'=>'yyyy-MM-dd HH:mm:ss','timestamp'=>'时间戳']))->build();

        $cloumn = \brand\Brand::Builder('日期类型','arrays')
            ->form_options([$options])
            ->form_props( [
                'append'    =>  $curd_method != 'curd_method'
            ])
            ->form_visible([['prop' => $curd_method, 'value' => 'Date']])
            ->build();


        $value_format = $tableConfigValue['form']['props']['value-format'] ?? 'default';
        $type = $tableConfigValue['form']['props']['type'] ?? 'datetime';

        return ['column' => $cloumn,'value' => [['type'=>$type,'value_format'=>$value_format]]];

    }

    public function methodFormConf($form,$table,$params)
    {
        $param = $params['param'][0] ?? [];
        $props['type'] = $param['type'] ?? 'datetime';

        $value_format = 'yyyy-MM-dd HH:mm:ss';
        if (!isset($param['value_format']) || $param['value_format'] != 'default'){
            $value_format = $param['value_format'];
        }
        $props['value-format'] = $value_format;
        $form['props'] = $props;

        if ($props['type'] == 'datetimerange'){
            $table['action_type'] = 'date';
            $table['type'] = 'datetimerange';
        }

        if (isset($params['auto_time']) && $params['auto_time'] != 'no'){
            $form[$params['auto_time']] = $form['props']['value-format'] ?? 'timestamp';
        }

        return [$form,$table];
    }



}